package com.meixiang.activity.homes.staging;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.adapter.home.StagingBindAdapter;
import com.meixiang.entity.StagingBinding;
import com.meixiang.main.BaseActivity;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author yinsujun  Created at 2016/5/5 11:14
 *         提升之信息绑定
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class PromoteForBindingActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.staging_binding_recycler)
    RecyclerView recycler;
    @Bind(R.id.staging_binding_tv_select_name)
    TextView tvSelectName;
    @Bind(R.id.staging_binding_et_account)
    ClearEditText bindingEtAccount;
    @Bind(R.id.staging_binding_et_password)
    ClearEditText etPassword;
    @Bind(R.id.staging_binding_tv_delete)
    TextView tvDelete;
    @Bind(R.id.staging_binding_tv_account)
    TextView tvAccount;
    @Bind(R.id.staging_binding_bt_submit)
    Button btSubmit;
    @Bind(R.id.staging_binding_linear_success)
    LinearLayout linearSuccess;
    @Bind(R.id.staging_binding_linear_input)
    LinearLayout linearInput;
    @Bind(R.id.staging_binding_linear_submit)
    LinearLayout linearSubmit;
    private String[] bindName = {"淘宝", "京东", "运营商", "学历认证", "邮箱", "征信"};
    private int[] bindIconId = {R.mipmap.taobao, R.mipmap.jd, R.mipmap.operator, R.mipmap.education, R.mipmap.email, R.mipmap.credit_investigation};
    private List<StagingBinding> stagingBindings;
    private StagingBindAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_promote_for_binding);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        setEvent();
    }

    @Override
    protected void initTitle() {
        setTitleBackground("信息绑定",R.mipmap.ic_white_back);
    }

    @Override
    protected void initView() {
        stagingBindings = new ArrayList<>();
        for (int i = 0; i < bindName.length; i++) {
            StagingBinding stagingBinding = new StagingBinding();
            stagingBinding.setBindName(bindName[i]);
            stagingBinding.setBindIcon(bindIconId[i]);
            stagingBinding.setBindId(i);
            stagingBindings.add(stagingBinding);
        }
        mAdapter = new StagingBindAdapter(context, stagingBindings);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycler.setLayoutManager(layoutManager);
        recycler.setAdapter(mAdapter);
        mAdapter.notifyData(0);

    }

    @Override
    protected void setData() {

    }
    private void initInputView(){
        linearSuccess.setVisibility(View.GONE);
        linearInput.setVisibility(View.VISIBLE);
        linearSubmit.setVisibility(View.VISIBLE);
        btSubmit.setBackground(ContextCompat.getDrawable(context,R.drawable.staging_gray_btn_background));
        btSubmit.setTextColor(ContextCompat.getColor(context,R.color.gray_b));
    }

    private void setEvent() {
        mAdapter.setOnItemClickListener(new StagingBindAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                initInputView();
                mAdapter.notifyData(position);
                StagingBinding binding = stagingBindings.get(position);
                if (binding.getBindName().equals("淘宝")) {
                    tvDelete.setVisibility(View.VISIBLE);
                } else {
                    tvDelete.setVisibility(View.GONE);
                }
                if (binding.getBindName().equals("运营商")) {
                    tvSelectName.setText(binding.getBindName() + "(电信/移动/联通)");
                    bindingEtAccount.setHint("本人正在使用的手机");
                    etPassword.setHint("服务密码");
                } else if (binding.getBindName().equals("征信")) {
                    tvSelectName.setText(binding.getBindName() + "中心");
                    bindingEtAccount.setText("已注册账户名");
                } else if (binding.getBindName().equals("邮箱")) {
                    tvAccount.setText("地址");
                    tvSelectName.setText("信用卡邮箱");
                    bindingEtAccount.setHint("邮箱地址");
                } else {
                    tvSelectName.setText(stagingBindings.get(position).getBindName() + "账户");
                }
                if (binding.getBindName().equals("京东")
                        || binding.getBindName().equals("淘宝")) {
                    bindingEtAccount.setHint("手机号/会员名/邮箱");
                }
                if (binding.getBindName().equals("学历认证")) {
                    bindingEtAccount.setHint("学信网账户");
                }


            }
        });
    }


    @OnClick({R.id.staging_binding_bt_submit})
    void click(View view) {
        if (view.equals(btSubmit)) {
            if (tvSelectName.getText().toString().equals("淘宝账户")) {
                linearSuccess.setVisibility(View.VISIBLE);
                linearInput.setVisibility(View.GONE);
                linearSubmit.setVisibility(View.GONE);
                btSubmit.setBackground(ContextCompat.getDrawable(context,R.drawable.staging_orange_btn_background));
                btSubmit.setTextColor(ContextCompat.getColor(context,R.color.white));
            }else{
                initInputView();
            }
        }

    }

    @Override
    public void onClick(View v) {

    }


}
